package SHIYAN;
import java.util.Arrays;
import java.util.Comparator;
    //1.复数类
    class ComplexNumber {
        private double real;
        private double imag;
        @Override
        public String toString() {
            return "ComplexNumber [real=" + real + ", imag=" + imag + "]";
        }
        public double getReal() {
            return real;
        }
        public double getImag() {
            return imag;
        }
        //默认构造方法
        public ComplexNumber() {
            this(1,1);
        }
        //带参数的构造方法
        public ComplexNumber(double real, double imag) {
            this.real = real;
            this.imag = imag;
        }
        public double getLength() {
            return Math.sqrt(real*real + imag*imag);
        }

    }
    //2.比较器类
//定义比较ComplexNumber对象的规则
    class ComplexNumberComparator implements Comparator{
        @Override
        public int compare(Object o1, Object o2) {
            // TODO Auto-generated method stub
            ComplexNumber c1 = (ComplexNumber)o1;
            ComplexNumber c2 = (ComplexNumber)o2;
            return (int)c1.getLength() - (int)c2.getLength();
        }

    }
    //3.测试类
    public class ComplexNumberTest {
        public static void main(String[] args) {
            //对象数组
            ComplexNumber[] arr = {
                    new ComplexNumber(2,8),
                    new ComplexNumber(1,1),
                    new ComplexNumber(2,3)
            };
            //输入数组和比较器参数
            Arrays.sort(arr, new ComplexNumberComparator() );
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }

